package com.zky.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.zky.DAO.impl.Imp1DaoImp;
import com.zky.entity.Imp1;


public class BuyCart extends Imp1DaoImp {
	
	String orderId="order"+ String.format("%tQ",new Date());
	String orderMsg;
	String orderMsg1;	
	public List<BuyItem> itemss;
	int total=0;
	int num;
	public Imp1 imp;
	int number=0;
	

	HttpServletRequest request = ServletActionContext.getRequest(); 
	HttpServletRequest session = ServletActionContext.getRequest();
	HttpSession s = request.getSession();
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	
	public String getOrderMsg1() {
		return orderMsg1;
	}
	public void setOrderMsg1(String orderMsg1) {
		this.orderMsg1 = orderMsg1;
	}
	public String getOrderMsg() {
		return orderMsg;
	}
	public void setOrderMsg(String orderMsg) {
		this.orderMsg = orderMsg;
	}
	
	public List<BuyItem> getItemss() {
		return itemss;
	}
	public void setItemss(List<BuyItem> itemss) {
		this.itemss = itemss;
	}
	/*public Imp1 imp=new Imp1();*/
	
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	
	 public Imp1 getImp() {
		return imp;
	}
	public void setImp(Imp1 imp) {
		this.imp = imp;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}

	@SuppressWarnings("unchecked")
	public final void addShoes(){
		
		int id=Integer.parseInt(request.getParameter("id"));
		List<BuyItem> items= (List<BuyItem>) s.getAttribute("kk");
		if(items==null) {
			items=new ArrayList<BuyItem>();
			BuyItem buyItem=new BuyItem();			
			  buyItem.setImp(this.findById(id));
			  buyItem.setAmount(1);
			  //buyItem.setAllprice(buyItem.getImp().getNewprice()*1);
			  items.add(buyItem);
			
			  System.out.print(items);
		}
		else{	
			boolean is = false;  
			BuyItem buyItem1=new BuyItem();
			for(int i=0;i<items.size();i++){
				buyItem1=items.get(i);
				if(buyItem1.getImp().getId()==id){
					buyItem1.setAmount(buyItem1.getAmount()+1);
					// buyItem1.setAllprice(buyItem1.getAmount()*buyItem1.getImp().getNewprice());
					
					items.set(i, buyItem1);
					 is = true;  
					  System.out.print("叠加");
					break;
				}
				
			}		
			if(!is ){
				BuyItem buyItem=new BuyItem();
				  buyItem.setAmount(1);
				  buyItem.setImp(this.findById(id));
				 // buyItem.setAllprice(buyItem.getImp().getNewprice()*1);
				
				  System.out.print("不相同");
				  items.add(buyItem);				
			}
			
		}	  
		    s.setAttribute("kk", items);;
		
	}
	@SuppressWarnings("unchecked")
	public String totalPrice(){
		
		BuyCart pp=new BuyCart();
		pp.addShoes();
		 itemss= (List<BuyItem>) s.getAttribute("kk");
		for (BuyItem i : itemss) {			
			total=total+i.getAmount()*i.getImp().getNewprice();
			number=number+i.getAmount();
			
			System.out.println(total);		
		}
		s.setAttribute("number", number);
		return "success";
		
	}
	@SuppressWarnings("unchecked")
	public String updateCart(){
		String usersname=(String) s.getAttribute("username");
		int iss=Integer.parseInt(request.getParameter("id"));
      int numm=Integer.parseInt(request.getParameter("num"));
		itemss=(List<BuyItem>) s.getAttribute("kk");
		for (BuyItem i : itemss) {
			if(i.getImp().getId()==iss){
				i.setAmount(numm);
				break;
			}
		}	
		 orderMsg="尊敬的"+usersname+"，您好，您的定单信息如下：<br />" ;
		 orderMsg1 = "尊敬的"+usersname+"，您好，您的定单信息如下：\n" ;
		 
		for (BuyItem i : itemss) {		
			orderMsg=orderMsg+i.getImp().getName()+ "," +i.getAmount()+ "件<br />";
			orderMsg1=orderMsg+i.getImp().getName()+ "," +i.getAmount()+ "件\n";
			total=total+i.getAmount()*i.getImp().getNewprice();
			number=number+i.getAmount();
			System.out.println(total);
			System.out.println(number);
			System.out.println(orderMsg);
		}		
		s.setAttribute("kk", itemss);
		s.setAttribute("number", number);
		return "success";	
	}
	
	@SuppressWarnings("unchecked")
	public String delcart(){
		String usersname=(String) s.getAttribute("username");
		int iss=Integer.parseInt(request.getParameter("id"));
		itemss=(List<BuyItem>) s.getAttribute("kk");
		for (BuyItem i : itemss) {
			if(i.getImp().getId()==iss){
				itemss.remove(i);
				break;
			}
		}	
		orderMsg="尊敬的"+usersname+"，您好，您的定单信息如下：<br />" ;
		 orderMsg1 = "尊敬的"+usersname+"，您好，您的定单信息如下：\n" ;
		for (BuyItem i : itemss) {	
			orderMsg=orderMsg+i.getImp().getName()+ "," +i.getAmount()+ "件<br />";
			orderMsg1=orderMsg+i.getImp().getName()+ "," +i.getAmount()+ "件\n";
			total=total+i.getAmount()*i.getImp().getNewprice();
			number=number+i.getAmount();
			System.out.println(number);
			System.out.println(total);
		}	
		s.setAttribute("kk", itemss);
		s.setAttribute("number", number);
		return "success";
		
	}
	
	@SuppressWarnings("unchecked")
	public String order(){
		String usersname=(String) s.getAttribute("username");
		if(usersname==null){
			return "false";
		}
		else{
			  orderMsg="尊敬的"+usersname+"，您好，您的定单信息如下：<br />" ;
			 orderMsg1 = "尊敬的"+usersname+"，您好，您的定单信息如下：\n" ;
			itemss= (List<BuyItem>) s.getAttribute("kk");
			for (BuyItem i : itemss) {			
				total=total+i.getAmount()*i.getImp().getNewprice();
				orderMsg=orderMsg+i.getImp().getName()+ "," +i.getAmount()+ "件<br />";
				orderMsg1=orderMsg+i.getImp().getName()+ "," +i.getAmount()+ "件\n";
				System.out.println(total);
			}
			return "success";
			
		}
		
		
	}
	
}
